package com.dingreading.cloud.soms.service.impl;

import com.dingreading.cloud.soms.dto.CaTeacherDto;
import com.dingreading.cloud.soms.entity.CaTeacher;
import com.dingreading.cloud.soms.entity.table.CaTeacherTableDef;
import com.dingreading.cloud.soms.mapper.CaTeacherMapper;
import com.dingreading.cloud.soms.service.CaTeacherService;
import com.mybatisflex.core.query.QueryCondition;
import com.mybatisflex.core.query.QueryWrapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 服务层实现。
 *
 * @author Administrator
 * @since 2023-11-22
 */
@Service
public class CaTeacherServiceImpl extends BaseServiceImpl<CaTeacherMapper, CaTeacher, CaTeacherTableDef> implements CaTeacherService {

    @Override
    protected CaTeacherTableDef getTable() {
        return CaTeacherTableDef.caTeacher;
    }

    @Override
    public int dropTeacher(String storeUid, String scheduleUid) {
        QueryCondition condition = table.storeUid.eq(storeUid)
                .and(table.scheduleUid.eq(scheduleUid));
        return getMapper().deleteByCondition(condition);
    }

    @Override
    public int dropTeacherByLessonUid(String storeUid, String scheduleUid, String lessonUid) {
        QueryCondition condition = table.storeUid.eq(storeUid)
                .and(table.scheduleUid.eq(scheduleUid))
                .and(table.lessonUid.eq(lessonUid));
        return getMapper().deleteByCondition(condition);
    }

    @Override
    public List<CaTeacherDto> getTeachersByScheduleUids(String storeUid, List<String> scheduleUids, Integer lessonStatus) {
        QueryCondition condition = table.scheduleUid.in(scheduleUids);
        if (StringUtils.isNotBlank(storeUid))
            condition.and(table.storeUid.eq(storeUid));
        if (null == lessonStatus)
            condition.and(table.lessonUid.isNull());

        QueryWrapper wrapper = QueryWrapper.create()
                .where(condition)
                .orderBy(table.teacherType.asc());
        return getMapper().selectListByQueryAs(wrapper, CaTeacherDto.class);
    }

    @Override
    public List<CaTeacherDto> getTeachers(String storeUid, String scheduleUid, Integer lessonStatus) {
        QueryCondition condition = table.storeUid.eq(storeUid)
                .and(table.scheduleUid.eq(scheduleUid));
        if (null == lessonStatus)
            condition.and(table.lessonUid.isNull());

        QueryWrapper wrapper = QueryWrapper.create().where(condition)
                .orderBy(table.teacherType.asc());
        return getMapper().selectListByQueryAs(wrapper, CaTeacherDto.class);
    }

}
